﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace GetEmptyFolders
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 0)
            {
                SPSite site = new SPSite(args[0].ToString());
                SPWebCollection webs = site.AllWebs;
                SPListCollection lists;
                SPListItemCollection folders;
                foreach (SPWeb web in webs)
                {
                    Console.WriteLine("Scanning Site: " + web.Title);
                    try 
                    {
                        lists = web.Lists;
                        foreach (SPList list in lists)
                        {
                            //Console.WriteLine("Scanning Library: " + list.Title);
                            try 
                            {
                                if (list.Hidden == false)
                                {
                                    if (list.ItemCount == 0)
                                    {
                                        Console.WriteLine("Empty Library: " + list.Title);
                                        Console.WriteLine("Url: " + list.DefaultViewUrl);
                                        Console.WriteLine("---------");
                                    }
                                    else if(list.BaseType == SPBaseType.DocumentLibrary)
                                    {
                                        SPFolder folder;
                                        folders = list.Folders;
                                        foreach (SPListItem item in folders)
                                        {
                                            folder = web.GetFolder(item.Url);
                                            if (folder.Files.Count == 0)
                                            {
                                                try
                                                {
                                                    Console.WriteLine("Found Folder: " + folder.Name);
                                                    Console.WriteLine("Library: " + list.Title);
                                                    Console.WriteLine("Url: " + folder.ServerRelativeUrl);
                                                    //Console.WriteLine(web.Title + "," + list.Title + "," + folder.Name + "," + folder.Files.Count.ToString());
                                                }
                                                catch (Exception) { }
                                            }
                                        }
                                    }
                                }
                            }
                            catch(Exception) {}
                        }
                        Console.WriteLine("---------");
                    }
                    catch(Exception) {}
                }
            }
        }
    }
}
